﻿using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using QuanLyGiaoVuService.CommonServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.ReportServices
{
    public class DangKyGioDayReportService : IDangKyGioDayReportService
    {
        public IDangKyGioDayRepository _dangKyGioDayRepository;
        public ITietHocService _tietHocService;

        public DangKyGioDayReportService(IDangKyGioDayRepository dangKyGioDayRepository, ITietHocService tietHocService)
        {
            _dangKyGioDayRepository = dangKyGioDayRepository;
            _tietHocService = tietHocService;
        }

        public QuanLyGiaoVuDAL.DTO.DangKyGioDayDTO GetThongTinDangKyGioDayById(Guid id)
        {
            return _dangKyGioDayRepository.GetDangKyGioDayById(id);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.DangKyGioDayDTO> GetAllThongTinDangKyGioDay()
        {
            return _dangKyGioDayRepository.GetAllDangKyGioday(null, null, null, null, null, null);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.DangKyGioDayDTO> GetAllThongTinDangKyGioDay(Guid giangVienId, Guid hocKyId)
        {
            return _dangKyGioDayRepository.GetAllDangKyGioday(giangVienId, null, null, null, null, hocKyId);
        }

        public IEnumerable<QuanLyGiaoVuDAL.DTO.DangKyGioDayDTO> GetAllThongTinDangKyGioDay(Guid? giangVienId, Guid? lopMonHocId, int? tietDauId, int? tietSauId, int? thu, Guid? hocKyId)
        {
            IEnumerable<DangKyGioDayDTO> result = _dangKyGioDayRepository.GetAllDangKyGioday(giangVienId, lopMonHocId, tietDauId, tietSauId, thu, hocKyId);
            return result;
        }

        public string KiemTraSapXep(string danhSachMaDK)
        {
            var danhSachDK = _dangKyGioDayRepository.GetThongTinDangKyGioDayByIdList(danhSachMaDK);
            List<DangKyGioDayDTO> danhSachDaXet = new List<DangKyGioDayDTO>();
            string message = string.Empty;
            foreach (DangKyGioDayDTO dangKyGioDayDTO in danhSachDK)
            {
                danhSachDaXet.Add(dangKyGioDayDTO);
                String messageItem = String.Empty;
                foreach (DangKyGioDayDTO dangKyGioDayDTO2 in danhSachDK)
                {
                    bool daXet = danhSachDaXet.FindIndex(item => item.MaThongTinGiangDay == dangKyGioDayDTO2.MaThongTinGiangDay) != -1;
                    if (!daXet && (dangKyGioDayDTO2.LopMonHocDTO.LopSinhVienId.Equals(dangKyGioDayDTO.LopMonHocDTO.LopSinhVienId) || dangKyGioDayDTO2.LopMonHocDTO.MaGiangVien1.Equals(dangKyGioDayDTO.LopMonHocDTO.MaGiangVien1)))
                    {
                        if (TrungGio(dangKyGioDayDTO, dangKyGioDayDTO2))
                        {
                            messageItem = messageItem + "," + dangKyGioDayDTO2.LopMonHocDTO.MaLopMonHoc + "-" + dangKyGioDayDTO2.LopMonHocDTO.MonHoc.TenMonHoc;
                            danhSachDaXet.Add(dangKyGioDayDTO2);
                        }
                    }
                }
                if (!String.IsNullOrEmpty(messageItem))
                {
                    messageItem = "Lớp " + dangKyGioDayDTO.LopMonHocDTO.MaLopMonHoc + " - " + dangKyGioDayDTO.LopMonHocDTO.MonHoc.TenMonHoc + " không thể trùng giờ với lớp " + messageItem.Trim(',') + ".\n\r";
                    message += messageItem;
                }
            }
            return message;
        }

        private bool TrungGio(DangKyGioDayDTO obj1, DangKyGioDayDTO obj2)
        {
            return ((obj1.Thu == obj2.Thu) && !(obj1.TietDauDTO.GioBatDau >= obj2.TietSauDTO.GioKetThuc || obj1.TietSauDTO.GioKetThuc <= obj2.TietDauDTO.GioBatDau));
        }


        public DangKyGioDayDTO[] SapXepDanhSachDangKyGioDay(IEnumerable<DangKyGioDayDTO> danhSachDKGioDay)
        {
            foreach (DangKyGioDayDTO dangKy in danhSachDKGioDay)
            {
                dangKy.ThuTuTrongTuan = _tietHocService.GetThuTuTietTrongTuan(dangKy.Thu, dangKy.TietDauDTO.TietThu);
            }
            danhSachDKGioDay.OrderBy(lop => lop.ThuTuTrongTuan);

            DangKyGioDayDTO[] dsDaSapXep = new DangKyGioDayDTO[danhSachDKGioDay.Count()];
            int currentIndex = 0;
            for (int thu = 2; thu <= 7; thu++)
            {
                bool sang = true;
                var dsLopTrongNgay = danhSachDKGioDay.Where(lop => lop.Thu == thu).ToList();
                while (dsLopTrongNgay.Count() > 0)
                {
                    int index = dsLopTrongNgay.FindIndex(lop => (lop.TietDauDTO.TietThu <= 5) == sang);
                    if (index >= 0)
                    {
                        sang = !sang;
                        dsDaSapXep[currentIndex++] = dsLopTrongNgay.ElementAt(index);
                        dsLopTrongNgay.RemoveAt(index);
                    }
                    else if (index < 0)
                    {
                        sang = !sang;
                    }
                }
            }
            return dsDaSapXep;
        }
    }
}
